package javaStu.Lambda;

import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author: leiwenkang02@meituan.com
 * @date: 2019/4/30
 * @time: 11:38
 */
public class Test02单例模式饿汉式链表相加两数相加 {

    @Test
    public void collect() {
        List<String> list = Stream.of("a", "b", "c")
                .collect(Collectors.toList());

        System.out.println(list);
    }

    //转换
    @Test
    public void map() {
        List<String> list = Stream.of("a", "b", "hello")
                .map(string -> string.toUpperCase())
                .collect(Collectors.toList());
        System.out.println(list);
    }

    //保留符合条件的元素
    @Test
    public void filter() {
        List<String> list =
                Stream.of("a", "1abc", "abc1")
                        .filter(value -> value.startsWith("a"))
                        .collect(Collectors.toList());
        System.out.println(list);
    }

    //合并多个collection
    @Test
    public void flatMap() {
        List<Integer> list =
                Stream.of(Arrays.asList(1, 2), Arrays.asList(3, 4))
                        .flatMap(numbers -> numbers.stream())
                        .collect(Collectors.toList());

        System.out.println(list);
    }

    //求数量
    @Test
    public void count() {
        long count = Stream.of("a", "b", "c")
                .count();

        System.out.println(count);
    }

    //求最大值
    @Test
    public void max() {
        int max = Stream.of(3, 6, 2, 8, 7, 1, 5)
                .max(Integer::compareTo)
                .get();

        System.out.println(max);
    }

    //求最小值
    @Test
    public void min() {
        int min = Stream.of(3, 6, 2, 8, 7, 1, 5)
                .min(Integer::compareTo)
                .get();

        System.out.println(min);
    }

    //累加 identity为初始值
    @Test
    public void reduce() {
        int result = Stream.of(1, 2, 3, 4)
                .reduce(0, (acc, element) -> acc + element);
        System.out.println(result);
    }

    //并行化操作 数据并行化是指将数据分成块，为每块数据分配单独的处理单元。这样可以充分利用多核 CPU 的优势。
    @Test
    public void parallel() {
        int sumSize = Stream.of("Apple", "Banana", "Orange", "Pear")
                .parallel()
                .map(s -> s.length())
                .reduce(Integer::sum)
                .get();
        System.out.println(sumSize);
    }

    //排序
    @Test
    public void sort() {
        List<Integer> list = Lists.newArrayList(3, 5, 1, 10, 8);
        List<Integer> sortedList = list.stream()
                .sorted(Integer::compareTo)
                .collect(Collectors.toList());
        System.out.println(sortedList);
    }




}
